package pl.nazaweb.jboot.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 *
 * @author Marek Kawczyński
 */
public class FileWriterUtils {

    public static void writeAllArray(int array[][], File file) {
        try (FileWriter fileWriter = new FileWriter(file)) {
            for (int x = 0; x < array.length; x++) {
                writeSingleLine(array, x, fileWriter);
                writeLineSeparator(fileWriter);
            }
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }

    public static void writeOnlyMapPoints(int array[][], File file) {
        try (FileWriter fileWriter = new FileWriter(file)) {
            for (int x = 0; x < array.length; x++) {
                writeSingleLine(array, x, fileWriter);
            }
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }

    public static void writeOnlyMapPointsSingleLine(int array[][], int x, FileWriter fileWriter) throws IOException {
        for (int y = 0; y < array.length; y++) {
            if (array[x][y] == 1) {
                String line = String.format("%s;%s", x, y);
                fileWriter.write(line);
                writeLineSeparator(fileWriter);
            }
        }
    }

    public static void writeSingleLine(int[][] array, int x, FileWriter fileWriter) throws IOException {
        for (int y = 0; y < array.length; y++) {
            fileWriter.write(array[x][y] + ";");
        }
    }

    public static void writeLineSeparator(FileWriter fileWriter) throws IOException {
        fileWriter.write(System.getProperty("line.separator"));
    }
}
